<?php
class SystemsController extends AppController {
	public $uses = array('Phong', 'User','Acrpermission','Acpermission','ChiTieu','Tthc','DanhMucPhanLoai');
	var $scaffold;
	
	public function beforeFilter () {
		$this->layout = 'system';
		if(!parent::isAllow('system')) {
			$this->redirect(array("controller" => 'denied', 'action' => 'index'));
		}
	}
	
	public function index() {
	}
	
	/**
	 * Khai bao don vi hanh chinh
	 */
	public function updateProfile () {
		$this->render("under_contruction");
	}
	public function khaiBaoTTHC () {
		$this->render("under_contruction");
	}
	
	public function danhMucPhanLoai () {
		$listPhanloais = $this->DanhMucPhanLoai->find("all");
		$this->set("phanloais", $listPhanloais);
	}
	
	public function addChiTieuPhanLoai() {
		
		if ($this->request->is('post')) {
			if ($this->DanhMucPhanLoai->save($this->data)) {
				// $this->Session->setFlash(__("Thêm chỉ tiêu thành công"));
				$this->redirect("danhMucPhanLoai");
			}
		}
	}
	
	public function editChiTieuPhanLoai($idChiTieu = null) {
		// debug($chiTieu);
		if ($this->request->is('post')) {
			// debug($this->data);
			$this->DanhMucPhanLoai->id = $this->request->data['DanhMucPhanLoai']['id'];
			
			$this->DanhMucPhanLoai->saveField('ten', $this->request->data['DanhMucPhanLoai']['ten']);
			$this->DanhMucPhanLoai->saveField('ma', $this->request->data['DanhMucPhanLoai']['ma']);
			
			$this->DanhMucPhanLoai->save();
			
			$this->redirect("danhMucPhanLoai");
		}
		
		if(empty($idChiTieu)) throw new NotFoundException();
		$chiTieu = $this->DanhMucPhanLoai->findById($idChiTieu);
		if(empty($chiTieu)) throw new NotFoundException();
		
		$this->request->data = $chiTieu;
		// debug($this->request->data);
		$tthcs = $this->Tthc->find("list", array('fields'=>array('id','ten'),'conditions' => array('activeFlg' => 1)));
		$this->set(compact('tthcs'));
	}
	
	public function xoaChiTieuPhanLoai($id = null) {
		if (!empty($id)) {
			if ($this->DanhMucPhanLoai->delete($id)) {
				$this->redirect("danhMucPhanLoai");
			}
		}
	}
	
	public function danhMucChiTieu () {
		$listChiTieus = $this->ChiTieu->find("all");
		$this->set("chitieus", $listChiTieus);
	}
	
	public function addChiTieu() {
		
		if ($this->request->is('post')) {
			if ($this->ChiTieu->save($this->data)) {
				// $this->Session->setFlash(__("Thêm chỉ tiêu thành công"));
				$this->redirect("danhMucChiTieu");
			} else {
				//$this->Session->setFlash(__("Thêm chỉ tiêu không thành công"));
			}
		}
		
		$tthcs = $this->Tthc->find("list", array('fields'=>array('id','ten'),'conditions' => array('activeFlg' => 1)));
		$this->set(compact('tthcs'));
	}
	
	public function editChiTieu($idChiTieu = null) {
		
		if ($this->request->is('post')) {
			// debug($this->data);
			$this->ChiTieu->id = $this->request->data['ChiTieu']['id'];
			
			$this->ChiTieu->saveField('ten', $this->request->data['ChiTieu']['ten']);
			$this->ChiTieu->saveField('ma', $this->request->data['ChiTieu']['ma']);
			$this->ChiTieu->saveField('don_vi_tinh', $this->request->data['ChiTieu']['don_vi_tinh']);
			$this->ChiTieu->saveField('tthc_id', $this->request->data['ChiTieu']['tthc_id']);
			
			$this->ChiTieu->save();
		}
		
		if(empty($idChiTieu)) throw new NotFoundException();
		$chiTieu = $this->ChiTieu->findById($idChiTieu);
		if(empty($chiTieu)) throw new NotFoundException();
		
		$this->request->data = $chiTieu;
		$tthcs = $this->Tthc->find("list", array('fields'=>array('id','ten'),'conditions' => array('activeFlg' => 1)));
		$this->set(compact('tthcs'));
	}
	
	public function xoaChiTieu($id = null) {
		if (!empty($id)) {
			if ($this->ChiTieu->delete($id)) {
				$this->redirect("danhMucChiTieu");
			}
		}
	}
	
	/**
	 * Quan ly phong ban
	 */
	 
	public function danhSachPhong() {
		$listPhongs = $this->Phong->find("all");
		
		$this->set("phongs", $listPhongs);
	}
	
	public function addPhong () {
		if ($this->request->is('post')) {
			// debug($this->data);
			if ($this->Phong->save($this->data)) {
				$this->Session->setFlash(__("Them phong thanh cong"));
				$this->redirect("danhSachPhong");
			} else {
				$this->Session->setFlash(__("Them phong khong thanh cong"));
			}
		}
	}
	
	public function editPhong ($phongId = null) {
		if (empty($phongId)) {
			$this->log("Invalid phong Id:"+$phongId);
			$this->redirect(array('controller'=>'pages','action'=>'home')); 
		} else {
			$phong = $this->Phong->findById($phongId);
			
			if (empty($phong)) {
				$this->log("Not found phong as request:"+$phongId);
				$this->redirect(array('controller'=>'pages','action'=>'home')); 
			}
			
			$this->set("phong", $phong);
		}
		
		
	}
	
	public function removePhong ($phongId = null) {
		
	}
	/** users add/delete ... **/
	public function manageUsers() {
		$users = $this->User->find("all");
		//debug($users);
		
		$this->set('users', $users);
	} 
	
	public function addUser() {
		$listPhongs = $this->Phong->find("list", array('fields'=>array('id','ten')));
		$this->set("phongs", $listPhongs);
		
		if (!empty($this->request->data)) {
			$this->request->data['User']['birth_day'] = '';
			if (!empty($this->request->data['User']['ngaysinh'])) {
				$date = $this->convertStrDate($this->request->data['User']['ngaysinh']);
				$this->request->data['User']['birth_day'] = $date;
			}
			
			$this->request->data['User']['company_id'] = 1;
			
			$this->User->create();
			$this->User->save($this->request->data);
			$this->redirect("manageUsers");
		}
	}
	
	public function editUser($username = null) {
		
		
		if (!empty($this->request->data)) {
			$user = $this->User->findByUsername($this->request->data['User']['username']);
			
			if(empty($user)) throw new NotFoundException();
			
			$this->User->id = $user['User']['id'];
			
			$this->User->saveField('phong_id',$this->request->data['User']['phong_id']);
			$this->User->saveField('ho_ten',$this->request->data['User']['ho_ten']);
			
			if (!empty($this->request->data['User']['ngaysinh'])) {
				$date = $this->convertStrDate($this->request->data['User']['ngaysinh']);
				
				$this->User->saveField('birth_day', $date);
			}
			
			$this->User->save();
			$this->redirect("manageUsers");
		}
		
		$listPhongs = $this->Phong->find("list", array('fields'=>array('id','ten')));
		
		$user = $this->User->findByUsername($username);
		// debug($user);
		if(empty($username)) throw new NotFoundException();
		
		$this->request->data['User'] = $user['User'];
		// debug($user['User']);
		$this->set("phongs", $listPhongs);
		$this->set('selectedPhong', $user['Phong']['id']);
	}
	
	public function removeUser($username = null) {
		$user = $this->User->findByUsername($username);
		
		if (!empty($user)) {
			$this->User->delete($user['User']['id']);
			$this->redirect(array('action' => 'manageUsers'));
		}
		
	}
	
	
	/**
	 * Dinh nghia vi tri cong tac
	 */
	 
	public function phanCong () {
		// update permissions
		if(!empty($this->request->data)) {
			$ps = $this->request->data['System'];
			
			$mapUsers = $this->User->find("list", array('fields'=>array('username','id')));
			$mapAcs = $this->Acpermission->find("list", array('fields'=>array('alias','id')));
			$permissions = $this->Acrpermission->loadPermissions();
			// debug($permissions);
			
			foreach ($ps as $key => $value) {
				$keyvalue = explode("_", $key);
				
				$uPer = $keyvalue[0];
				$u = $keyvalue[1];
				
				// debug($this->Acrpermission->permissions);
				$arcPermission = array();
				$this->request->data['Acrpermission']['user_id'] = $mapUsers[$u];
				$this->request->data['Acrpermission']['acpermission_id'] = $mapAcs[$uPer];
				$this->request->data['Acrpermission']['comment'] = $u;
				
				if ($value == 1 && empty($permissions[$u][$uPer])){
					$this->log('There is no permission: '.$uPer."|".$u);
					
					$this->Acrpermission->setPermission($this->request->data);
					$this->log('Set permission for: %1', $u);
					
				} else if ($value == 0 && !empty($permissions[$u][$uPer])){
					$this->Acrpermission->removePermission($this->request->data['Acrpermission']);
					$this->log('Remove permission from: '.$u);
				} 
			}
		}

		// load permission table
		
		$ps = $this->Acrpermission->find("all");
		$acs = $this->Acpermission->find("all");
		$users = $this->User->find("all");
		
		foreach ($ps as $key => $p) {
			$this->request->data['System'][$p['Acpermission']['alias']."_".$p['User']['username']] = 1;
		}
		
		$this->set("acs", $acs);
		$this->set('users', $users);
	}
}
